package com.erpanderp.wam.asset.view;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.primefaces.context.RequestContext;
import org.primefaces.model.SelectableDataModel;

import com.erpanderp.wam.asset.model.entity.AssetDetails;
import com.erpanderp.wam.asset.model.entity.AssetHistory;
import com.erpanderp.wam.asset.model.entity.AssetMaster;
import com.erpanderp.wam.asset.model.entity.AssetRequest;
import com.erpanderp.wam.asset.model.service.AssetHistoryDAO;
import com.erpanderp.wam.asset.model.service.AssetMasterDAO;
import com.erpanderp.wam.asset.model.service.AssetRequestDAO;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.ValidationUtil;

@ManagedBean
@ViewScoped
public class AssetAssignedBySysAdminBean implements Serializable,SelectableDataModel<AssetMaster>{


	private static final long serialVersionUID = 1L;
	private HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
	ServletContext context = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
	HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
	private TimeZone timeZone = TimeZone.getDefault();
	
	private List<AssetMaster> allAssetsList=new ArrayList<AssetMaster>();
	
	private AssetMaster selectedAsset;
	private AssetRequest assetRequest=new AssetRequest();
	private AssetDetails assetDetails=new AssetDetails();
	private AssetHistory assetHistory=new AssetHistory();
	
	private AssetMasterDAO assetMasterDAO=new AssetMasterDAO();
	private AssetRequestDAO assetRequestDAO =new AssetRequestDAO();
	private AssetHistoryDAO assetHistoryDAO=new AssetHistoryDAO();
	
	private String assetType;
	private String assetCode;
	private String assetDetailsId;
	private String assignedRemarks;
	private int Sno=1;
	private boolean FORM_VALID;
	

	public AssetAssignedBySysAdminBean() {
		
		assetType=request.getParameter("assetType");
		assetCode = request.getParameter("assetCode");
		assetDetailsId = request.getParameter("assetDetailsId");
		
		assetDetails= assetRequestDAO.getAssetDetailsByAssetDetailsId(Long.parseLong(assetDetailsId));
		assetRequest = assetRequestDAO.getAssetRequestByAssetCode(assetCode);
		allAssetsList = assetMasterDAO.getAllAssetsByAssetStatus(assetType, "In Store");
		
		for(AssetMaster assetMaster:allAssetsList){
			assetMaster.setSno(Sno);
			Sno++;
		}
	}

	
	 public void validateAction(ActionEvent e)  {
			
		 validateForm();
		if(FORM_VALID){	 
		   RequestContext.getCurrentInstance().execute("confirmation.show();");
		}else{
			RequestContext.getCurrentInstance().execute("confirmation.hide();");
			RequestContext.getCurrentInstance().execute("assetDialog.show();");
		}
	 }
	
	public String assetAssignmentAction(){	
		validateForm();
		if(FORM_VALID){	 
		    selectedAsset.setAssetRequest(assetRequest);
		    selectedAsset.setAssignedDate(new Date());
		    selectedAsset.setAssignedRemarks(assignedRemarks);
		    selectedAsset.setAssetStatus("IN USE");
						 
			assetMasterDAO.saveOrUpdateAssetMaster(selectedAsset);	
			//======
			assetDetails.setAssetAssignedStatus("ASSIGNED");
			assetDetails.setAssetMaster(selectedAsset);
			
			assetRequestDAO.updateAssetDetails(assetDetails);
			//========
			assetHistory.setAssetRequest(assetRequest);
			assetHistory.setAssetMaster(selectedAsset);
			assetHistory.setAssetDetails(assetDetails);
			assetHistory.setAssetAssignedBy(assetRequest.getAssigedByEmployee());
			assetHistory.setAssetAssignedDate(new Date());
			assetHistory.setAssetStatus("IN USE");
			assetHistory.setCreateDate(new Date());
			assetHistory.setCreatedBy(assetRequest.getAssigedByEmployee().getFirstName()+" "+assetRequest.getAssigedByEmployee().getLastName());
			assetHistory.setStatus(CommonLookup.STATUS_ACTIVE);
			assetHistoryDAO.saveOrUpdateAssetHistory(assetHistory);
		
			FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("assetCode", assetCode);
			try{
				FacesContext.getCurrentInstance().getExternalContext().redirect("/wam/asset/assignassets.html?assetCode="+assetCode);
			}catch (IOException e) {
				e.printStackTrace();
			}
			  return "assignassets";	
		}else{
			//RequestContext.getCurrentInstance().execute("confirmation.show();");
			return null;
		}
    }
	private void validateForm(){
		FORM_VALID=true;
		if(ValidationUtil.isEmpty(getAssignedRemarks(), "Remarks ", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}
		
	}
	@Override
	public AssetMaster getRowData(String rowKey) {          
        for(AssetMaster assetMaster : allAssetsList) {  
            if(assetMaster.getAssetCode().equals(rowKey))  
                return assetMaster;  
        }  		
		return null;
	}

	@Override
	public Object getRowKey(AssetMaster assetMaster) {		
		return assetMaster.getAssetCode();
	}

	//GENERATE GETTERS AND GETTERS
	public HttpSession getSession() {
		return session;
	}

	public void setSession(HttpSession session) {
		this.session = session;
	}

	public TimeZone getTimeZone() {
		return timeZone;
	}

	public void setTimeZone(TimeZone timeZone) {
		this.timeZone = timeZone;
	}

	public List<AssetMaster> getAllAssetsList() {
		return allAssetsList;
	}

	public void setAllAssetsList(List<AssetMaster> allAssetsList) {
		this.allAssetsList = allAssetsList;
	}

	public String getAssetType() {
		return assetType;
	}

	public void setAssetType(String assetType) {
		this.assetType = assetType;
	}

	public int getSno() {
		return Sno;
	}

	public void setSno(int sno) {
		Sno = sno;
	}

	public AssetMaster getSelectedAsset() {
		return selectedAsset;
	}

	public void setSelectedAsset(AssetMaster selectedAsset) {
		this.selectedAsset = selectedAsset;
	}

	public String getAssetCode() {
		return assetCode;
	}

	public void setAssetCode(String assetCode) {
		this.assetCode = assetCode;
	}

	public AssetRequest getAssetRequest() {
		return assetRequest;
	}

	public void setAssetRequest(AssetRequest assetRequest) {
		this.assetRequest = assetRequest;
	}

	public String getAssignedRemarks() {
		return assignedRemarks;
	}

	public void setAssignedRemarks(String assignedRemarks) {
		this.assignedRemarks = assignedRemarks;
	}


	public String getAssetDetailsId() {
		return assetDetailsId;
	}


	public void setAssetDetailsId(String assetDetailsId) {
		this.assetDetailsId = assetDetailsId;
	}


	public AssetDetails getAssetDetails() {
		return assetDetails;
	}


	public void setAssetDetails(AssetDetails assetDetails) {
		this.assetDetails = assetDetails;
	}


	public AssetHistory getAssetHistory() {
		return assetHistory;
	}


	public void setAssetHistory(AssetHistory assetHistory) {
		this.assetHistory = assetHistory;
	}


	public AssetHistoryDAO getAssetHistoryDAO() {
		return assetHistoryDAO;
	}


	public void setAssetHistoryDAO(AssetHistoryDAO assetHistoryDAO) {
		this.assetHistoryDAO = assetHistoryDAO;
	}

	

	

}
